<?php

/**
 * Created by PhpStorm.
 * User: lyang
 * Date: 2016/12/31
 * Time: 23:35
 */
class Cache
{
	private $path;
	public function __construct($url){
		$this->path = $url;
		if(!is_dir($this->path)){
			mkdir($this->path);
		}
	}
	public function _begin($one,$debug=false){
		if(!$debug){
				$i_path = $this->path."/".hash("tiger192,3",$one);  
			if(is_file( $i_path )){
				$time = filemtime ($i_path);
				if(time()-$time>7200){
					goto SAVE;
				}  else{ 
					exit(YC::html(file_get_contents($i_path)));
				}
			}
			
			SAVE:
				ob_start();
		} 
	}
	public function _end($one,$debug=false){
		$i_path = $this->path."/".hash("tiger192,3",$one); 
		$html = ob_get_contents();
		file_put_contents($i_path,$html);
		ob_end_flush();
	}
	public function refresh() { 
	  $dir = $this->path;
	  $dh=opendir($dir);
	  while ($file=readdir($dh)) {
	    if($file!="." && $file!="..") {
	      $fullpath=$dir."/".$file;
	      if(!is_dir($fullpath)) { 
	          unlink($fullpath);
	      }  
	    }
	  }
	}
}